<template>
<!-- <div>弃用</div> -->
    <el-dialog
        :title="!dataForm.id ? $t('handle.add') : $t('handle.update')"
        :close-on-click-modal="false"
        :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="所在楼栋" >
          <el-cascader v-model="buildingAddress" ref="build" :options="buildingOptions" @click.native="getCommunty" @active-item-change="getbuilding" :props="buildingProps" style="width:100%;"></el-cascader>
      </el-form-item>
      <el-form-item label="单元号" prop="unitNumber">
        <el-input v-model="dataForm.unitNumber" placeholder="单元号"></el-input>
      </el-form-item>
      <el-form-item label="楼层号" prop="floorCode">
        <el-input v-model="dataForm.floorCode" placeholder="楼层号"></el-input>
      </el-form-item>
      <el-form-item label="位置号" prop="positionNumber">
        <el-input v-model="dataForm.positionNumber" placeholder="位置号"></el-input>
      </el-form-item>
      <el-form-item label="门牌号" prop="houseNumber">
        <el-input v-model="dataForm.houseNumber" placeholder="门牌号"></el-input>
      </el-form-item>
       <el-form-item label="是否出租" prop="isLet">
          <el-select style="width: 100%;" v-model="dataForm.isLet" filterable placeholder="请选择" >
            <el-option v-for="item in dicts" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="房屋地址" prop="dgStandardInfoId">
        <el-input v-model="dataForm.dgStandardInfoId" placeholder="房屋地址"></el-input>
      </el-form-item> -->
      <el-form-item label="房屋分类" prop="housingClassification">
        <el-select style="width: 100%;" v-model="dataForm.housingClassification" filterable placeholder="请选择" >
            <el-option v-for="item in housingClassificationDicts" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="管理者" prop="managerId">
        <el-input v-model="dataForm.managerId" placeholder="管理者"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="callOf()">{{ $t('handle.cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('handle.confirm') }}</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
    data(){
        return {
            dicts:[],
            housingClassificationDicts: [],
            visible: false,
            dataForm: {
              id: '',
              buildingId: '',
              unitNumber:'',
              floorCode:'',
              positionNumber:'',
              houseNumber:'',
              dgStandardInfoId: '',
              housingClassification: '',
              isLet: '',
              managerId: '',
            },
            buildingAddress: [],
            buildingOptions: [],
            buildingProps: {
                value: "id",
                label: "name",
                children: "children"
            },
            // 验证后期确定了再加上
            dataRule: {
                buildingId: [
                    {required: false, message: '楼栋不能为空', trigger: 'change'}
                ],
                unitNumber: [
                    {required: true, message: '单元号不能为空', trigger: 'blur'}
                ],
                floorCode: [
                    {required: true, message: '楼层号不能为空', trigger: 'blur'}
                ],
                positionNumber: [
                    {required: true, message: '位置号不能为空', trigger: 'blur'}
                ],
                houseNumber: [
                    {required: true, message: '门牌号不能为空', trigger: 'blur'}
                ],
                dgStandardInfoId: [
                    {required: false, message: '房屋地址不能为空', trigger: 'blur'}
                ],
                housingClassification: [
                    {required: true, message: '房屋分类不能为空', trigger: 'blur'}
                ],
                isLet: [
                    {required: true, message: '是否出租不能为空', trigger: 'blur'}
                ],
                managerId: [
                    {required: true, message: '管理者不能为空', trigger: 'blur'}
                ]
            }
        }
    },
    mounted: function(){
        // this.getCountry()
    },
    methods: {
         // 楼栋
        getbuilding(val){
            if(val.length === 1){
                this.getProvice(val[0])
            }else if(val.length === 2){
                this.getCity(val[0],val[1])
            }else if(val.length === 3){
                this.getArea(val[0],val[1],val[2])
            }else if(val.length === 4){
                this.getCommunty(val[0],val[1],val[2],val[3])
            }else if(val.length === 5){
                this.getHousing(val[0],val[1],val[2],val[3],val[4])
            }else if(val.length === 6){
                this.getBuild(val[0],val[1],val[2],val[3],val[4],val[5])
            }
        },
        getCountry(){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: 0,type:11}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                    this.buildingOptions = data.data

                }
            })
        },
        getProvice(countryId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: countryId,type:12}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                   this.buildingOptions.map(item=>{
                       if(item.id === countryId){
                           item.children = data.data
                       }
                   })
                }
            })
        },
        getCity(countryId,provinceId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: provinceId,type:13}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                   this.buildingOptions.map((item, i) => {
                        if (item.id === countryId) {
                            item.children.map((city, id) => {
                                 city.children = data.data;
                            });
                        }
                    });
                }
            })
        },
        getArea(countryId,provinceId,cityId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: cityId,type:31}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                    this.buildingOptions.map((item, i) => {
                        if (item.id === countryId) {
                        item.children.map((provice, idx) => {
                            if(provice.id === provinceId){
                                provice.children.map((city, ids) => {
                                    city.children = data.data
                                });
                            }
                        });
                        }
                    });
                }

            })
        },
        getCommunty(countryId,provinceId,cityId,areaId){
            this.$http({
                url: `/dg-standard-info/dgstandardaddress/childlistbyparentandtype`,
                method: 'post',
                params:{parentId: areaId,type:32}
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                    this.buildingOptions.map((item, i) => {
                        if (item.id === countryId) {
                        item.children.map((provice, idx) => {
                            if(provice.id === provinceId){
                                provice.children.map((city, ids) => {
                                  if(city.id === cityId){
                                    city.children.map((area,aid)=>{
                                      area.children =data.data
                                    })
                                  }
                                });
                            }
                        });
                        }
                    });
                }
            })
        },
        getHousing(countryId,provinceId,cityId,areaId,communityCodeId){
            this.$http({
                url: `/dg-ic/ic/housing-estate/getByCommuntId/${communityCodeId}`,
                method: 'get'
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        this.$set(item, "children", []);
                    });
                    this.buildingOptions.map((item, i) => {
                        if (item.id === countryId) {
                        item.children.map((provice, idx) => {
                            if(provice.id === provinceId){
                                provice.children.map((city, ids) => {
                                    if(city.id === cityId){
                                        city.children.map((area,aid)=>{
                                            if(area.id === areaId){
                                                area.children.map((communt,ic)=>{
                                                    communt.children = data.data
                                                })
                                            }
                                        })
                                    }
                                });
                            }
                        });
                        }
                    });
                }

            })
        },
        getBuild(countryId,provinceId,cityId,areaId,communityCodeId,housId){
            this.$http({
                url: `/dg-ic/ic/building/getByHousing/${housId}`,
                method: 'get'
            }).then(({data})=>{
                if(data && data.code === 0){
                    data.data.map(item => {
                        this.$set(item, "name", item.name);
                        // this.$set(item, "children", []);
                    });
                    this.buildingOptions.map((item, i) => {
                        if (item.id === countryId) {
                        item.children.map((provice, idx) => {
                            if(provice.id === provinceId){
                                provice.children.map((city, ids) => {
                                    if(city.id === cityId){
                                        city.children.map((area,aid)=>{
                                            if(area.id === areaId){
                                                area.children.map((communt,ic)=>{
                                                    if(communt.id === communityCodeId){
                                                        communt.children.map((house,iu)=>{
                                                            house.children = data.data
                                                        })
                                                    }
                                                })
                                            }
                                        })
                                    }
                                });
                            }
                        });
                        }
                    });
                }

            })
        },
        init(id,dicts,housingClassificationDicts){
            this.dicts = dicts
            this.housingClassificationDicts = housingClassificationDicts
            this.visible = true
            this.dataForm.id = id || 0
            if(this.dataForm.id){
                this.$http({
                    url: `/dg-ic/ic/room/info/${this.dataForm.id}`,
                    method: 'get',
                    params: this.$http.adornParams()
                }).then(({data})=>{
                    if(data && data.code === 0){
                        // this.dataForm.buildingId = data.data.buildingId
                        this.buildingOptions = [{
                            id: data.data.buildingId,
                            name: data.data.buildingName
                        }];
                        this.buildingAddress = [data.data.buildingId];
                        this.dataForm.unitNumber = data.data.unitNumber
                        this.dataForm.floorCode = data.data.floorCode
                        this.dataForm.positionNumber = data.data.positionNumber
                        this.dataForm.houseNumber = data.data.houseNumber
                        this.dataForm.dgStandardInfoId = data.data.dgStandardInfoId
                        this.dataForm.housingClassification = String(data.data.housingClassification)
                        this.dataForm.isLet = String(data.data.isLet)
                        this.dataForm.managerId = data.data.managerId
                    }
                })
            }
        },
        dataFormSubmit(){
                this.$refs['dataForm'].validate((validate)=>{
                 if(validate){
                     this.$http({
                        url: `/dg-ic/ic/room/${!this.dataForm.id ? 'save' : 'update'}`,
                        method: 'post',
                        data: this.$http.adornData({
                            'id': this.dataForm.id || undefined,
                            'buildingId': this.buildingAddress[this.buildingAddress.length-1],
                            'unitNumber': this.dataForm.unitNumber,
                            'floorCode': this.dataForm.floorCode,
                            'positionNumber': this.dataForm.positionNumber,
                            'houseNumber': this.dataForm.houseNumber,
                            'dgStandardInfoId': this.dataForm.dgStandardInfoId,
                            'housingClassification': this.dataForm.housingClassification,
                            'isLet': this.dataForm.isLet,
                            'managerId': this.dataForm.managerId,
                            'createBy': this.$store.state.user.id,
                            'updateBy': this.$store.state.user.id
                        })
                     }).then(({data})=>{
                         if(data && data.code === 0){
                            this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                                let obj = {}
                                obj.stopPropagation = () =>{}
                                this.$refs.build.clearValue(obj)
                                this.$refs[`dataForm`].resetFields()
                                }
                            })
                         }else{
                            this.$message.console.error(data.msg);
                        }
                     })
                 }
             })
        },

        callOf(){
            this.visible = false
            let obj = {}
            obj.stopPropagation = () =>{}
            this.$refs.build.clearValue(obj)
            this.$refs[`dataForm`].resetFields()
        }
    }
}
</script>

